package com.kou.dao;

import com.kou.pojo.Cart;
import com.kou.pojo.Goods;
import com.kou.utils.DataSourceUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author SteinY
 * @create 2022-03-25 15:52
 * @description:
 */
public class CartDaoImpl implements CartDao {
    QueryRunner queryRunner = new QueryRunner();
    @Override
    public void create(String pid, int uid, int price) {
        int pid1 = Integer.parseInt(pid);
        Connection connection = DataSourceUtil.getConnection();
        String sql = "insert into tb_cart values (0,?,?,1,?)";
        Object [] params = {uid,pid1,price};
        try {
            queryRunner.update(connection,sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Cart> findCarts(String uid) {
        List<Cart> carts = new ArrayList<>();
        Connection connection = DataSourceUtil.getConnection();
        String sql  = "SELECT * FROM tb_cart tc  INNER JOIN tb_goods tg on tc.pid=tg.id WHERE tc.uid = ? ;";
        try {
            List<Map<String, Object>> maps = queryRunner.query(connection, sql, new MapListHandler(), uid);
            for (Map<String, Object> map : maps) {

                Cart cart = new Cart();
                Goods goods = new Goods();
                BeanUtils.copyProperties(cart,map);
                BeanUtils.copyProperties(goods,map);

                cart.setGoods(goods);
                carts.add(cart);
            }
            return carts;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void delete(String cid) {
        Connection connection = DataSourceUtil.getConnection();
        String sql = "delete from tb_cart where cid = ?";
        try {
            queryRunner.update(connection,sql,cid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(String cid, String cnum, String price) {

        Connection connection = DataSourceUtil.getConnection();
        String sql = "update tb_cart set num = ? ,money = ? where cid = ? ";
        try {
            queryRunner.update(connection,sql,Integer.parseInt(cnum),Integer.parseInt(cnum)*Integer.parseInt(price),cid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void clear(String uid) {
        Connection connection = DataSourceUtil.getConnection();
        String sql = "delete from tb_cart where uid = ?";
        try {
            queryRunner.update(connection,sql,uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Cart findCart(String pid, int uid) {
        Connection connection = DataSourceUtil.getConnection();
        String sql = "select * from tb_cart where uid = ? and pid = ? ";
        try {
            return queryRunner.query(connection,sql,new BeanHandler<>(Cart.class),uid,pid);

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
